package day09.handler.simpleChannelHandler;

import day09.packet.MessageRequestPacket;
import day09.packet.MessageResponsePacket;
import day09.util.PacketCodeC;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Date;

/**
 * @author chenshijie
 * @date 2023/12/26 13:20
 */

public class MessageRequestHandler extends SimpleChannelInboundHandler<MessageRequestPacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageRequestPacket messageRequestPacket) throws Exception {
        ctx.channel().writeAndFlush(receiveMessage(messageRequestPacket));
    }

    private static MessageResponsePacket receiveMessage(MessageRequestPacket messageRequestPacket) {
        MessageResponsePacket messageResponsePacket = new MessageResponsePacket();
        messageResponsePacket.setVersion(messageRequestPacket.getVersion());

        System.out.println(new Date() + ": 收到客户端消息: " + messageRequestPacket.getMessage());

        messageResponsePacket.setMessage("服务段回复:【" + messageRequestPacket.getMessage() + "】");

        return messageResponsePacket;
    }
}
